import Taro from '@tarojs/taro';
import { create } from 'zustand';
import { immer } from 'zustand/middleware/immer';
import { createDemand } from '@/business/services/demand/demand';

/**
 * @description 发布需求
 * @author Shawn
 */

interface Params {
  type: string;
  cityCode?: string;
  cityName: string;
  tag?: string;
  title: string;
  description: string;
  fileList?: any[];
  phonePrivacy?: string;
  resourceUrgency?: string;
  neverExpire: boolean;
  expireTime?: string;
}

type RequestFunction = (params: Params) => Promise<void>;

type State = {
  // 发布需求回调
  demandCreateRes: any;
};

type Actions = {
  // 发布需求
  getdemandsCreateRes: RequestFunction;
};

const initialState: State = {
  demandCreateRes: {},
};

export const demandCreateStore = create<State & Actions>()(
  immer((set) => ({
    ...initialState,

    /**
     * 需求发布
     * @param type 发布类型
     * @param cityCode 地区代码
     * @param cityName 展示地区名称（省市县拼接）
     * @param tag 标签
     * @param title 需求标题
     * @param description 需求描述
     * @param fileList 附件
     * @param phonePrivacy 手机隐私
     * @param resourceUrgency 资源紧急程度
     * @param neverExpire 是否长期有效
     * @param expireTime 过期时间
     *
     */
    getdemandsCreateRes: async (params: Params) => {
      try {
        Taro.showLoading({
          title: '加载中',
        });
        const requestData = {
          ...params,
        };
        const res = await createDemand(requestData);

        if (res.success) {
          set((state) => {
            state.demandCreateRes = res.data;
          });
          Taro.showToast({ title: '发布成功', icon: 'success' });
        }
      } catch (error) {
        console.log('获取需求发布回调失败', error);
      } finally {
        // Taro.hideLoading();
      }
    },
  })),
);
